<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 * $Id$
 */

if(!defined('__CORE_DIR')){
    exit("Access Denied");
}

class Ctl_Weixin_Gz_Site extends Ctl_Weixin
{
    //上报阶段和工地日记保持一致
    //protected $pm_pro  = array(0=>'未开工',1=>'方案设计',2=>'水泥改造',3=>'泥瓦阶段',4=>'木工阶段',5=>'油漆阶段',6=>'完工',7=>'验收完成');
    protected $pm_pro = array(1=>'开工大吉',2=>'水电改造',3=>'泥瓦工阶段',4=>'木工阶段',5=>'油漆阶段',6=>'安装',7=>'验收完成');
    protected $pm_type = array(1=>'水电工程',2=>'土木工程',3=>'油漆工程',4=>'安装工程');

    public function index()
    {
        $openid = $this->access_openid();
        if(!$member = K::M('member/weixin')->detail_by_openid($openid)){
            $this->err->add('账号不存在或者未绑定微信',1000);
            $this->tmpl = 'weixin/gz/site/bind.html';
        }elseif('gz' != $member['from']){
            $this->err->add('您不是工长，不能添加工地日记',1000);
        }
        else{
            //print_r($member); exit;
            $map   = array('gz_uid'=>$member['uid'],'closed'=>0);
            $items = K::M('home/site')->items($map,array('dateline'=>'desc'));
            $this->pagedata['items'] = $items;
            $this->pagedata['steps'] = $this->pm_pro;
            $this->tmpl = 'weixin/gz/site/index.html';
        }
    }

    public function detail($site_id=null)
    {
        if(!$site_id){
            $this->err->add('非法访问',1000);
        }elseif(!$detail=K::M('home/site')->detail($site_id)){
            $this->err->add('非法访问',1000);
        }else{
            //工地详情
            $this->pagedata['detail'] = $detail;
            //日记
            $map = array('site_id'=>$site_id);
            $items = K::M('home/item')->items($map,array('dateline'=>'desc','status'=>'desc'));
            $this->pagedata['progress'] = $items;
            $this->pagedata['steps'] = $this->pm_pro;
            $this->tmpl = 'weixin/gz/site/detail.html';
        }
    }

    public function item($item_id=null, $site_id=null)
    {
        if(!$site_id || !$item_id){
            $this->err->add('非法访问',1000);
        }else{
            $detail  = K::M('home/site')->items(array('site_id'=>$site_id));
            $item = K::M('home/item')->items(array('item_id'=>$item_id));
            $this->pagedata['detail'] = pos($detail);
            $this->pagedata['item'] = pos($item);
            $this->pagedata['steps'] = $this->pm_pro;
            //print_r($item);
            //exit;
            $this->tmpl = 'weixin/gz/site/item.html';
        }
    }

    public function additem($site_id=null)
    {
        if(!$site_id){
            $this->err->add('非法访问',1000);
        }elseif($d=$this->GP('data')){
            if(!$d['title']){
                $this->err->add('请填写标题',1000);
            }elseif(!$files = $_FILES['img']){
                $this->err->add('请上传图片',1000);
            }else{
                //上传图片
                $root   = $_SERVER['DOCUMENT_ROOT'];
                $attach = '/attachs/homepics/'.date('Ym').'/';
                if(!file_exists($root.$attach)){ mkdir($root.$attach,0777); }

                foreach($files['name'] as $k=>$file_name){
                    $ext = explode('.', $file_name);
                    $des = $attach.date('Ymd').'_'.md5($files['tmp_name'][$k]).'.'.end($ext);
                    if(!move_uploaded_file($files['tmp_name'][$k], $root.$des)){
                        break;
                    }else{
                        $result[] = $des;
                    }
                }
                $d['photo_array'] = implode(',',$result);
                $d['clientip'] = __IP;
                $d['content']='无';
                if($item_id=K::M('home/item')->create($d, true)){
                    $this->err->add('操作成功');
                    //$this->err->set_data('forward', $this->mklink("weixin/zxpm/site:index"));
                    $this->err->set_data('forward', 'http://zjzs.com.cn/weixin/gz/site-detail-'.$site_id.'.html');
                    //$this->tmpl = 'weixin/zxpm/site/index.html';
                }else{
                    $this->err->add('操作失败了',1000);
                }
            }
        }else{
            $detail  = K::M('home/site')->detail($site_id);
            $this->pagedata['detail']= $detail;
            $this->pagedata['steps'] = $this->pm_pro;
            $this->pagedata['type']  = $this->pm_type;
            $this->tmpl = 'weixin/gz/site/addItem.html';
        }
    }
    public function bind()
    {
        $openid     = $this->cookie->get('wx_openid');
        $nickname   = $this->cookie->get($openid.'_nickname');
        $headimgurl = $this->cookie->get($openid.'_headimgurl');
        $session =K::M('system/session')->start();
        if(!$d=$this->GP('data')){
            $this->err->add('非法访问', 211);
        }else if(!$mobile = K::M('verify/check')->mobile($d['mobile'])){
            $this->err->add('手机号码不正确', 212);
        }else if($session->get("MOBILE_VERIFY_CODE") != $d['verify']){
            $this->err->add('验证码不正确', 213);
        }else if(!$supervist = K::M('supervist/supervist')->detail_by_mobile($mobile)){
            $this->err->add('手机码不存在', 214);
        }else if(K::M('supervist/supervist')->update($supervist['supervist_id'],array('openid'=>$openid))){
            $this->err->add('微信绑定成功,可通过微信管理工地');
        }else{
            $this->err->add('微信绑定失败', 215);
        }
    }


    public function sendverify()
    {
        if(!$mobile = $this->GP('mobile')){
            $this->err->add('手机号码不正确', 211);
        }else if(!$mobile = K::M('verify/check')->mobile($mobile)){
            $this->err->add('手机号码不正确', 212);
        }else if(!$supervist = K::M('supervist/supervist')->detail_by_mobile($mobile)){
            $this->err->add('手机码不存在', 213);
        }else{
            $code = K::M('content/string')->Random(6, 1);
            $session =K::M('system/session')->start();
            $session->set('MOBILE_VERIFY_CODE',$code, 300); //5分钟有效
            K::M('sms/sms')->send($mobile,'mobile_verify',array('code'=>$code),$this->company_id);
            $this->err->add('发送验证码成功');
            //exit(json_encode(array('msg'=>'发送验证码成功,'.$session->get('MOBILE_VERIFY_CODE') )));
        }
    }

    private function upload($files,$progress_id)
    {
        $result = array();
        $root   = $_SERVER['DOCUMENT_ROOT'];
        $attach = '/attachs/photo/'.date('Ym').'/';
        if(!file_exists($root.$attach)){mkdir($root.$attach,0777);}
        foreach($files['name'] as $k=>$file_name){
            $ext = explode('.', $file_name);
            $des = $attach.date('Ymd').'_'.md5($files['tmp_name'][$k]).'.'.end($ext);
            if(!move_uploaded_file($files['tmp_name'][$k], $root.$des)){
                break;
            }else{
                $result[] = array('thumb'=>$des,'progress_id'=>$progress_id);
            }
        }
        return $result;
    }

    public function delete($item_id=null, $site_id=null)
    {
        if(!$site_id || !$item_id){
            $this->err->add('非法访问',1000);
        }elseif(!$detail=K::M('home/site')->detail($site_id)){
            $this->err->add('非法访问',1000);
        }else{
            K::M('home/item')->delete($item_id);
            $this->err->add('删除日记成功');
            //工地详情
            $this->pagedata['detail'] = $detail;
            //日记
            $map = array('site_id'=>$site_id);
            $items = K::M('home/item')->items($map,array('dateline'=>'desc','status'=>'desc'));
            $this->pagedata['progress'] = $items;
            $this->pagedata['steps'] = $this->pm_pro;
            $this->tmpl = 'weixin/gz/site/detail.html';
        }
    }

}